
//判断是否是Email
function isEmail(email) {
    var regexp = /^[a-zA-Z0-9]+([_\.\-]\w+)*@\w+([_\.]\w+)*\.\w+([_\.]\w+)*$/;
    return regexp.test(email);
}

//判断是否是手机号
function isMobile(mobile) {
    //var regexp = /^(((13[0-9]{1})|(14[0-9]{1})|(15[0-9]{1})|(16[0-9]{1})|(17[0-9]{1})|(18[0-9]{1})|(19[8-9]{1}))+\d{8})$/;
    var regexp = /^[0-9]{6,11}$/;
    if (regexp.test(mobile))
        return true;
    regexp =  /^\+[0-9]{1,4}\-[0-9]{6,12}$/;
    return regexp.test(mobile);
}

//查询传递参数
function GetQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null)
        return unescape(r[2]);
    return null;
}

//设置cookie
function setCookie(c_name, value, expiredays) {
    var exdate = new Date()
    exdate.setDate(exdate.getDate() + expiredays)
    document.cookie = c_name + "=" + escape(value) +
        ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString())
}

//获取cookie
function getCookie(c_name) {
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(c_name + "=")
        if (c_start != -1) {
            c_start = c_start + c_name.length + 1
            c_end = document.cookie.indexOf(";", c_start)
            if (c_end == -1) c_end = document.cookie.length
            return unescape(document.cookie.substring(c_start, c_end))
        }
    }
    return ""
}

//是否是移动端
function isWapPage() {
    if (navigator.userAgent.match(/(Windows Phone|WPDesktop|iPhone|iPod|iPad|Android)/i)) {
        return true;
    }
    return false;
}

//是否是移动端（不包含Pad）
function isWapPageNoPad() {
    if (navigator.userAgent.match(/(Windows Phone|WPDesktop|iPhone|iPod|Android)/i)) {
        return true;
    }
    return false;
}

//url编码
function URLencode(sStr) {
    return sStr.replace(/\+/g, '%2B');
}

function showNationCode(t) {
    var node = $(t).parent();
    node.css('position', 'relative').css('border', '1px solid #333');
}

$(function() {
    function hideNationCode(t) {
        $(".nation_code_list").fadeOut(200);
        $(t).removeClass("expand");
        $('.form_style').unbind('click');
    }
    function showNationCode(t) {
        $(".nation_code_list").fadeIn(200);
        $(t).addClass("expand");
        setTimeout(function() {
            $('.form_style').bind('click',
                function() {
                    hideNationCode(t);
                }
            )},
        300);
    }

    $(".nation_code").click(function() {
        
        if ($(this).hasClass("expand")) {
            hideNationCode(this);
        } else {
            if ($(".nation_code_list").length === 0) {
                var that = this;
                $.ajax({
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    url: "/api/sms/nativecodes",
                    dataType: "json",
                    cache: false,
                    error: function() {
                        ShowErrorMessage("获取电话国家代码失败！");
                    },
                    success: function(re) {
                        if (re.success) {
                            var html = "<ul class='nation_code_list'>";
                            for (var i = 0; i < re.content.list.length; i++) {
                                html += "<li data-code='+"+re.content.list[i].code+"'>" +re.content.list[i].name + " +"+re.content.list[i].code + "</li>";
                            }
                            html += "</ul>";
                            $(that).parent().append(html);
                        } else {
                            ShowErrorMessage(re.message);
                        }

                        $(".nation_code_list li").click(function() {
                            $(".nation_code").text($(this).data("code"));
                            hideNationCode(that);
                        });
                    }
                });
            }
            showNationCode(this);
        }
    });
});